function myReplace(str, before, after) {
    return str.replace(before, $1 => {
        if (/^[A-Z]/.test($1)) {
            return after.replace(/^\w/, $2 => $2.toUpperCase());
        }
        return after;
    });
}

myReplace("A quick brown fox Jumped over the lazy dog", "Jumped", "leaped");
